import formJs from "./regexDetail.js"; //新增弹框配置项
import publicForm from "@/components/public/el-form.vue"; //弹框form

import {
    addClosedFormLedger, //闭合保存
    getSampleWorkByFid, //详情
    SamplegetStationWarnByCompanyId, //获取审核流程
} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: "闭合处理",
            isLock: false,
            dialogHeight: null,

            // 基本信息
            basicFormObj: formJs.basicFormObj,
            basicFormData: {},
            // 基本信息

            // 闭合
            // activeIndex: "1",
            activeNames: ["0"],
            closeFormData: {
                fId: "", // 申请单fid //为空判断为新增
                fCompanyid: "", // 施工标段id
                fParentid: "", // 对应的超标记录fid
                fWarningcontent: "", // 预警内容
                fReason: "", // 问题原因
                fHandleway: "", // 处理方式
                fHandleresult: "", // 处理结果
                fHandledate: "",
                fHandledates: "", // 处理日期
                fHandleusername: "", // 处理人名称
                fHandlefile: "", // 附件

                fFirstcheckresult: "", // 一级审核处理结果
                fFirstcheckopinion: "", // 一级审核处理意见
                fFirstcheckusername: "", // 一级审核处理人name
                fFirstcheckdates: "", // 一级审核处理日期
                fFirstcheckfile: "", // 一级审核附件

                fSecondcheckresult: "", // 二级审核处理结果
                fSecondcheckopinion: "", // 二级审核处理意见
                fSecondcheckusername: "", // 二级审核处理人name
                fSecondcheckdates: "", // 二级审核处理日期
                fSecondcheckfile: "", // 二级审核附件
            },
            // 闭合申请单
            handNum: false, //闭合申请单
            closeFormObj: formJs.closeFormObj, //闭合
            closeFileurllist: [], //附件

            // 闭合申请单
            //  一级审核
            firstNum: false, //一级审核
            FauditFormObj: formJs.Faudit, //一级审核
            FauditFileurllist: [], //附件
            //  一级审核

            //  二级审核
            secondNum: false, //二级审核
            SauditFormObj: formJs.Saudit, //二级审核
            SauditFileurllist: [],
            //  二级审核
            btnShow: true,
            // 闭合
            newRow: null,


        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },

        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },

        statusNum: {
            handler(newVal) {
                if (newVal) {

                    this.edit(newVal);
                }
            },
            immediate: true,
        },

        currentRow: {
            handler(newVal) {
                this.newRow = newVal;
            },
            deep: true,
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        this.btnShow = true;
        this.getDetail(this.newRow);
        if (this.statusNum == 1) { //闭合

        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.handNum = true; //闭合申请单
            this.firstNum = true; //一级审核
            this.secondNum = true; //二级审核
            // 闭合
            this.closeFormObj.map((item) => {
                item.disabled = true;
            });
            // 闭合
            // 一级审核
            this.FauditFormObj.map((item) => {
                item.disabled = true;
            });
            // 一级审核
            //  二级闭合
            this.SauditFormObj.map((item) => {
                item.disabled = true;
            });
            this.closeFormObj[3].uploadSelect = false
            this.FauditFormObj[4].uploadSelect = false
            this.SauditFormObj[4].uploadSelect = false
            //  二级闭合
        }
        this.getHeight();
    },
    methods: {
        // 折叠面板
        changeCollapse(e) {
            console.log(this.activeNames, e);
        },
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            console.log("selectChange", data, a, b);
        },

        edit() {
            this.closeFormData = this.$options.data.call(this).closeFormData;
        },

        // 详情
        getDetail(row) {
            this.loading = true;
            getSampleWorkByFid({
                fId: row.fId
            }).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    this.recordfiles = [];
                    this.basicFormData = res.data.lrCheckSample; //基本信息
                    this.closeFormData = res.data.checkSampletestclose; //闭合 
                   
                    this.closeFileurllist = res.data.fCloseSourcefileList; //闭合申请单材料List
                    this.FauditFileurllist = res.data.fFirstSourcefileList; //一级审核材料List
                    this.SauditFileurllist = res.data.fSecondSourcefileList; //二级审核材料List

                    this.getExamine(row)
                } else {
                    this.$message.error("列表数据获取失败", res.msg);
                    this.loading = false;
                }

            });
        },
        getExamine(data) {
            var status = data.fCheckstatus;
            SamplegetStationWarnByCompanyId({
                    fCompanyid: data.fCompanyid
                })
                .then((res) => {
                    if (res.errcode == 0) {
                        var arr = res.data.yjQmpMsCheckSamplewarn.fAduitUser1.split("-");
                        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
                        this.btnShow = true;
                        if ((this.userInfo.companyNature == arr[status])) {
                            if (status == 0) {
                                // 闭合申请单 可编辑
                                this.handNum = true; //闭合申请单
                                this.firstNum = false; //一级审核
                                this.secondNum = false; //二级审核
                                this.activeNames = ["0"];
                                if (this.closeFormData.fHandledates == null || this.closeFormData.fHandledates == "") {
                                    this.closeFormData.fHandledates = this.common.day(0);
                                    this.closeFormData.fHandleusername = this.userInfo.realName; //处理人
                                }
                                // 闭合申请单
                                this.closeFormObj.map((item) => {
                                    item.disabled = true;
                                });
                                this.closeFormObj[1].disabled = false;
                                // 闭合申请单
                                // 一级审核
                                this.FauditFormObj.map((item) => {
                                    item.disabled = true;
                                });
                                // 一级审核
                                //  二级闭合
                                this.SauditFormObj.map((item) => {
                                    item.disabled = true;
                                });
                                //  二级闭合
                                this.closeFormObj[4].uploadSelect = true
                                this.FauditFormObj[4].uploadSelect = false
                                this.SauditFormObj[4].uploadSelect = false
                            } else if (status == 1) {
                                // 一级审核
                                this.handNum = true; //闭合申请单
                                this.firstNum = true; //一级审核
                                this.secondNum = false; //二级审核
                                this.activeNames = ["0", "1"];

                                // 闭合申请单
                                this.closeFormObj.map((item) => {
                                    item.disabled = true;
                                });
                                // 闭合申请单

                                // 一级审核
                                this.FauditFormObj.map((item) => {
                                    item.disabled = false;
                                });
                                this.FauditFormObj[2].disabled = true;
                                this.FauditFormObj[3].disabled = true;
                                if (this.closeFormData.fFirstcheckdates == null || this.closeFormData.fFirstcheckdates == "") {
                                    this.closeFormData.fFirstcheckdates = this.common.day(0)
                                    this.closeFormData.fFirstcheckusername = this.userInfo.realName; //一级审核人姓名
                                }
                                // 一级审核


                                //  二级闭合
                                this.SauditFormObj.map((item) => {
                                    item.disabled = true;
                                });
                                //  二级闭合
                                this.closeFormObj[4].uploadSelect = false
                                this.FauditFormObj[4].uploadSelect = true
                                this.SauditFormObj[4].uploadSelect = false

                            } else if (status == 2) {
                                //    二级审核
                                this.handNum = true; //闭合申请单
                                this.firstNum = true; //一级审核
                                this.secondNum = true; //二级审核
                                this.activeNames = ["0", "1", "2"];

                                // 闭合
                                this.closeFormObj.map((item) => {
                                    item.disabled = true;
                                });
                                // 闭合
                                // 一级审核
                                this.FauditFormObj.map((item) => {
                                    item.disabled = true;
                                });

                                // 一级审核
                                //  二级闭合
                                this.SauditFormObj.map((item) => {
                                    item.disabled = false;
                                });
                                this.SauditFormObj[2].disabled = true;
                                this.SauditFormObj[3].disabled = true;
                                if (this.closeFormData.fSecondcheckdates == null || this.closeFormData.fSecondcheckdates == "") {
                                    this.closeFormData.fSecondcheckdates = this.common.day(0)
                                    this.closeFormData.fSecondcheckusername = this.userInfo.realName; //一级审核人姓名
                                }
                                //  二级闭合
                                this.closeFormObj[4].uploadSelect = false
                                this.FauditFormObj[4].uploadSelect = false
                                this.SauditFormObj[4].uploadSelect = true
                            } else if (status == 3) {
                                this.btnShow = false

                                this.handNum = true; //闭合申请单
                                this.firstNum = true; //一级审核
                                this.secondNum = true; //二级审核
                                // 闭合
                                this.closeFormObj.map((item) => {
                                    item.disabled = true;
                                });
                                // 闭合
                                // 一级审核
                                this.FauditFormObj.map((item) => {
                                    item.disabled = true;
                                });
                                // 一级审核
                                //  二级闭合
                                this.SauditFormObj.map((item) => {
                                    item.disabled = true;
                                });
                                this.closeFormObj[4].uploadSelect = false
                                this.FauditFormObj[4].uploadSelect = false
                                this.SauditFormObj[4].uploadSelect = false
                                //  二级闭合
                            }
                        } else {
                            this.btnShow = false
                            // 闭合
                            this.closeFormObj.map((item) => {
                                item.disabled = true;
                            });
                            // 闭合
                            // 一级审核
                            this.FauditFormObj.map((item) => {
                                item.disabled = true;
                            });
                            // 一级审核
                            //  二级闭合
                            this.SauditFormObj.map((item) => {
                                item.disabled = true;
                            });
                            //  二级闭合

                            this.closeFormObj[4].uploadSelect = false
                            this.FauditFormObj[4].uploadSelect = false
                            this.SauditFormObj[4].uploadSelect = false

                            if (status == 0) {
                                this.handNum = true; //闭合申请单
                                this.firstNum = false; //一级审核
                                this.secondNum = false; //二级审核
                            } else if (status == 1) {
                                this.activeNames = ["0"];
                                this.handNum = true; //闭合申请单
                                this.firstNum = false; //一级审核
                                this.secondNum = false; //二级审核

                            } else if (status == 2) {
                                this.activeNames = ["0", "1"]
                                this.handNum = true; //闭合申请单
                                this.firstNum = true; //一级审核
                                this.secondNum = false; //二级审核

                            } else if (status == 3) {
                                this.activeNames = ["0", "1", "2"]
                                this.handNum = true; //闭合申请单
                                this.firstNum = true; //一级审核
                                this.secondNum = true; //二级审核

                            }
                        }
                    }
                })
                .catch((error) => {
                    console.log(error);
                });
        },
        // 刪除文件
        delteFile(file, status) {
            // var obj;
            var arr;
            if (status == 0) {
                // obj = this.closeFormData;
                arr = this.closeFileurllist;
            } else if (status == 1) {
                // obj = this.FauditFormData;
                arr = this.FauditFileurllist;
            } else if (status == 2) {
                // obj = this.SauditFormData;
                arr = this.SauditFileurllist;
            }
            for (let index = 0; index < arr.length; index++) {
                if (file.uid == arr[index].uid) {
                    if (arr[index].atta == undefined) {
                        // obj.recordfiles.push({
                        this.recordfiles.push({
                            fId: arr[index].fId,
                        });
                    } else {
                        this.recordfiles.push({
                            // obj.recordfiles.push({
                            fId: arr[index].atta.fId,
                        });
                    }
                    this.closeFileurllist.splice(index, 1);
                }
            }
        },

        yj_saveForm(data) {
            var save = {
                fId: data.fId, // 申请单fid //为空判断为新增
                fCompanyid: this.newRow.fCompanyid, // 施工标段id
                fParentid: this.newRow.fId, // 对应的超标记录fid
                fWarningcontent: data.fWarningcontent, // 不合格参数及结果
                fReason: data.fReason, // 问题原因
                fHandleway: data.fHandleway, // 处理方式
                fHandleresult: data.fHandleresult, // 处理结果
                fHandledate: data.fHandledate,
                fHandledates: data.fHandledates, // 处理日期
                fHandleusername: data.fHandleusername, // 处理人名称
                fHandlefile: data.fHandlefile, // 附件

                fFirstcheckresult: data.fFirstcheckresult, // 一级审核处理结果
                fFirstcheckopinion: data.fFirstcheckopinion, // 一级审核处理意见
                fFirstcheckusername: data.fFirstcheckusername, // 一级审核处理人name
                fFirstcheckdates: data.fFirstcheckdates, // 一级审核处理日期
                fFirstcheckfile: data.fFirstcheckfile, // 一级审核附件

                fSecondcheckresult: data.fSecondcheckresult, // 二级审核处理结果
                fSecondcheckopinion: data.fSecondcheckopinion, // 二级审核处理意见
                fSecondcheckusername: data.fSecondcheckusername, // 二级审核处理人name
                fSecondcheckdates: data.fSecondcheckdates, // 二级审核处理日期
                fSecondcheckfile: data.fSecondcheckfile, // 二级审核附件

                aduitStatus: this.newRow.fCheckstatus, // 保存状态
                userId: this.userInfo.userId, // 当前登录人id
                userName: this.userInfo.realName, // 当前登录人姓名
                recordfiles: this.recordfiles, // 删除文件id集合
            }

            if (this.newRow.fCheckstatus == "0") {
                if (this.$refs.publicForm0.submitForm()) {
                    this.saveForm(save);
                } else {
                    console.log("error");
                }
            } else if (this.newRow.fCheckstatus == "1") {
                if (this.$refs.publicForm1.submitForm()) {
                    this.saveForm(save);
                } else {
                    console.log("error");
                }
            } else if (this.newRow.fCheckstatus == "2") {
                if (this.$refs.publicForm2.submitForm()) {
                    this.saveForm(save);
                } else {
                    console.log("error");
                }
            } else {
                console.log("error");
            }

        },
        // 保存
        saveForm(obj) {
            this.loading = true;

            this.isLock = true
            addClosedFormLedger(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });
        },
    },
};